package com.sinbader.xiaole.common.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import com.sinbader.xiaole.common.entity.InStockInfoDetail;
import com.sinbader.xiaole.common.mapper.InStockInfoDetailMapper;
import com.sinbader.xiaole.common.model.params.InStockInfoDetailParam;
import com.sinbader.xiaole.common.model.result.InStockInfoDetailResult;
import  com.sinbader.xiaole.common.service.InStockInfoDetailService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * 入库单（采购入库、调拨入库）明细 服务实现类
 */
@Service
public class InStockInfoDetailServiceImpl extends ServiceImpl<InStockInfoDetailMapper, InStockInfoDetail> implements InStockInfoDetailService {

    @Override
    public void add(InStockInfoDetailParam param){
        InStockInfoDetail entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(InStockInfoDetailParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(InStockInfoDetailParam param){
        InStockInfoDetail oldEntity = getOldEntity(param);
        InStockInfoDetail newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public InStockInfoDetailResult findBySpec(InStockInfoDetailParam param){
        return null;
    }

    @Override
    public List<InStockInfoDetailResult> findListBySpec(InStockInfoDetailParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(InStockInfoDetailParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(InStockInfoDetailParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private InStockInfoDetail getOldEntity(InStockInfoDetailParam param) {
        return this.getById(getKey(param));
    }

    private InStockInfoDetail getEntity(InStockInfoDetailParam param) {
        InStockInfoDetail entity = new InStockInfoDetail();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
